#import "WBPopMenuView.h"
#import "WBTableViewDataSource.h"
#import "WBTableViewDelegate.h"
#import "WBTableViewCell.h"
#import "WBPopMenuModel.h"

@interface WBPopMenuView () {
    CGPoint _leftTop;
}
@property (nonatomic, strong) WBTableViewDataSource * tableViewDataSource;
@property (nonatomic, strong) WBTableViewDelegate   * tableViewDelegate;

@end


@implementation WBPopMenuView

- (void) showMenu:(CGRect)menuFrame
            items:(NSArray *)items
           action:(void(^)(NSInteger index))action
{
    {
        _leftTop = menuFrame.origin;
        self.menuWidth = menuFrame.size.width;
        self.menuItem = items;
        self.action = [action copy];

        self.tableViewDataSource = [[WBTableViewDataSource alloc]initWithItems:items cellClass:[WBTableViewCell class] configureCellBlock:^(WBTableViewCell *cell, WBPopMenuModel *model) {
            UIFont *f = [UIFont fontWithName:@"DejaVu Sans Mono" size:14];
            WBTableViewCell * tableViewCell = (WBTableViewCell *)cell;
            tableViewCell.textLabel.text = model.title;
            tableViewCell.textLabel.font = f;
            tableViewCell.imageView.image = [UIImage imageNamed:model.image];
        }];
        self.tableViewDelegate = [[WBTableViewDelegate alloc]initWithDidSelectRowAtIndexPath:^(NSInteger indexRow) {
            if (self.action) {
                [self hideView];
                self.action(indexRow);
            }
        }];

        self.tableView = [[UITableView alloc]initWithFrame:[self menuFrame] style:UITableViewStylePlain];
        self.tableView.dataSource = self.tableViewDataSource;
        self.tableView.delegate   = self.tableViewDelegate;
        self.tableView.bounces = NO;
        self.tableView.rowHeight = 40;
        self.tableView.layer.cornerRadius = 10.0f;
        self.tableView.layer.anchorPoint = CGPointMake(0, 0);
        //[self.tableView.layer setBorderWidth:1];
        //[self.tableView.layer setBorderColor:[[UIColor grayColor] CGColor]];
        self.tableView.transform = CGAffineTransformMakeScale(0.0001, 0.0001);
        self.tableView.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.tableView];
        
        if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
            [self.tableView setSeparatorInset:UIEdgeInsetsZero];
        }
        if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
            [self.tableView setLayoutMargins:UIEdgeInsetsZero];
        }
        self.backgroundColor = [[UIColor blackColor]colorWithAlphaComponent:0.1];
    }
    
    UIWindow * window = [[[UIApplication sharedApplication] windows] firstObject];
    
    [window addSubview:self];
    
    [UIView animateWithDuration:0.3 animations:^{
        self.tableView.transform = CGAffineTransformMakeScale(1.0, 1.0);
    }];
}

- (CGRect)menuFrame {
    return  CGRectMake(_leftTop.x-0.5*self.menuWidth, _leftTop.y-20*self.menuItem.count, self.menuWidth, 40*self.menuItem.count);
}

#pragma mark 绘制三角形
- (void)drawRect:(CGRect)rect
{
    [[UIColor whiteColor] set];
    
    //拿到当前视图准备好的画板
    CGContextRef  context = UIGraphicsGetCurrentContext();
    
    //利用path进行绘制三角形
    CGContextBeginPath(context);//标记
    CGFloat location = _leftTop.x + 40;
    CGContextMoveToPoint(context, location, _leftTop.y);//设置起点
    CGContextAddLineToPoint(context, location - 10,  _leftTop.y-10);
    CGContextAddLineToPoint(context, location - 20, _leftTop.y);
    CGContextClosePath(context);//路径结束标志，不写默认封闭
    
    [[UIColor whiteColor] setFill];  //设置填充色
    [[UIColor whiteColor] setStroke]; //设置边框颜色
    
    CGContextDrawPath(context, kCGPathFillStroke);//绘制路径path
}
- (void)hideView {
    [UIView animateWithDuration:0.15 animations:^{
        self.tableView.transform = CGAffineTransformMakeScale(0.0001, 0.0001);
    } completion:^(BOOL finished) {
        [self.tableView removeFromSuperview];
        [self removeFromSuperview];
        self.tableView = nil;
    }];
}

- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self hideView];
}

@end
